package com.lysb.modules.controller;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.lysb.core.entity.ResponseEntity;
import com.lysb.core.utils.*;
import com.lysb.modules.entity.*;
import com.lysb.modules.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
public class BusinessController {
    @Value("${lysb.encryption.encryptionKey}")
    private String encryptionKey;
    @Autowired
    private BusinessService service;

    private String encryptionStr;
    private String decryptionStr;
    private String json;
    /**
     * ========================================快赔部分==========================================
     * =====================================包括门诊和住院========================================
     */
    /**
     * 门诊基本信息查询
     * @param param
     * @return
     */
    @PostMapping("/treatmentRecord")
    public ResponseEntity treatmentRecord(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        TreatmentRecordParam treatmentRecordParam = JSONUtil.toBean(decryptionStr, TreatmentRecordParam.class);
        if(Objects.isNull(treatmentRecordParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }

        List<TreatmentRecordData> treatmentRecordDataList = service.treatmentRecord(treatmentRecordParam);
        return unifyResp(treatmentRecordDataList);
    }
    /**
     * 门诊收费明细
     * @param param
     * @return
     */
    @PostMapping("/outpatientPrescription")
    public ResponseEntity outpatientPrescription(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        OutpatientPrescriptionParam outpatientPrescriptionParam = JSONUtil.toBean(decryptionStr, OutpatientPrescriptionParam.class);
        if(Objects.isNull(outpatientPrescriptionParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }

        List<OutpatientPrescriptionData> outpatientPrescriptionDataList = service.outpatientPrescription(outpatientPrescriptionParam);
        return unifyResp(outpatientPrescriptionDataList);
    }
    /**
     * 门诊费用结算
     * @param param
     * @return
     */
    @PostMapping("/outpatientSettle")
    public ResponseEntity outpatientSettle(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        OutpatientSettleParam outpatientSettleParam = JSONUtil.toBean(decryptionStr, OutpatientSettleParam.class);
        if(Objects.isNull(outpatientSettleParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }

        List<OutpatientSettleData> outpatientSettleDataList = service.outpatientSettle(outpatientSettleParam);
        return unifyResp(outpatientSettleDataList);
    }
    /**
     * 门诊病历信息
     * @param param
     * @return
     */
    @PostMapping("/outpatientInformation")
    public ResponseEntity outpatientInformation(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        OutpatientInformationParam outpatientInformationParam = JSONUtil.toBean(decryptionStr, OutpatientInformationParam.class);
        if(Objects.isNull(outpatientInformationParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }

        List<OutpatientInformationData> outpatientInformationDataList = service.outpatientInformation(outpatientInformationParam);
        return unifyResp(outpatientInformationDataList);
    }
    /**
     * 住院基本信息查询
     * @param param
     * @return
     */
    @PostMapping("/hospitalizationRecord")
    public ResponseEntity hospitalizationRecord(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        HospitalizationRecordParam hospitalizationRecordParam = JSONUtil.toBean(decryptionStr, HospitalizationRecordParam.class);
        if(Objects.isNull(hospitalizationRecordParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<HospitalizationRecordData> hospitalizationRecordDataList = service.hospitalizationRecord(hospitalizationRecordParam);
        return unifyResp(hospitalizationRecordDataList);
    }
    /**
     * 住院收费明细
     * @param param
     * @return
     */
    @PostMapping("/hospitalizationPrescription")
    public ResponseEntity hospitalizationPrescription(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        HospitalizationPrescriptionParam hospitalizationPrescriptionParam = JSONUtil.toBean(decryptionStr, HospitalizationPrescriptionParam.class);
        if(Objects.isNull(hospitalizationPrescriptionParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<HospitalizationPrescriptionData> hospitalizationPrescriptionDataList = service.hospitalizationPrescription(hospitalizationPrescriptionParam);
        return unifyResp(hospitalizationPrescriptionDataList);
    }
    /**
     * 住院费用结算
     * @param param
     * @return
     */
    @PostMapping("/hospitalizationSettle")
    public ResponseEntity hospitalizationSettle(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        HospitalizationSettleParam hospitalizationSettleParam = JSONUtil.toBean(decryptionStr, HospitalizationSettleParam.class);
        if(Objects.isNull(hospitalizationSettleParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<HospitalizationSettleData> hospitalizationSettleDataList = service.hospitalizationSettle(hospitalizationSettleParam);
        return unifyResp(hospitalizationSettleDataList);
    }
    /**
     * 住院出院记录
     * @param param
     * @return
     */
    @PostMapping("/admissionRecord")
    public ResponseEntity admissionRecord(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        AdmissionRecordParam admissionRecordParam = JSONUtil.toBean(decryptionStr, AdmissionRecordParam.class);
        if(Objects.isNull(admissionRecordParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<AdmissionRecordData> admissionRecordDataList = service.admissionRecord(admissionRecordParam);
        return unifyResp(admissionRecordDataList);
    }
    /**
     * 住院出院记录
     * @param param
     * @return
     */
    @PostMapping("/dischargeRecord")
    public ResponseEntity dischargeRecord(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        DischargeRecordParam dischargeRecordParam = JSONUtil.toBean(decryptionStr, DischargeRecordParam.class);
        if(Objects.isNull(dischargeRecordParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<DischargeRecordData> dischargeRecordDataList = service.dischargeRecord(dischargeRecordParam);
        return unifyResp(dischargeRecordDataList);
    }
    /**
     * 手术信息记录
     * @param param
     * @return
     */
    @PostMapping("/surgicalRecord")
    public ResponseEntity surgicalRecord(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        SurgicalRecordParam surgicalRecordParam = JSONUtil.toBean(decryptionStr, SurgicalRecordParam.class);
        if(Objects.isNull(surgicalRecordParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<SurgicalRecordData> surgicalRecordDataList = service.surgicalRecord(surgicalRecordParam);
        return unifyResp(surgicalRecordDataList);
    }
    /**
     * 疾病信息记录
     * @param param
     * @return
     */
    @PostMapping("/diagnosisRecord")
    public ResponseEntity diagnosisRecord(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        DiagnosisRecordParam diagnosisRecordParam = JSONUtil.toBean(decryptionStr, DiagnosisRecordParam.class);
        if(Objects.isNull(diagnosisRecordParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }
        List<DiagnosisRecordData> diagnosisRecordDataList = service.diagnosisRecord(diagnosisRecordParam);
        return unifyResp(diagnosisRecordDataList);
    }

    /**
     * 统一返回格式
     * @param obj
     * @param <T>
     * @return
     */
    public <T> ResponseEntity unifyResp(Object obj) {
        if(Objects.isNull(obj)) {
            return ResponseEntity.NO_DATA_FOUND();
        }
        if(obj instanceof List) {
            List<T> tList = (List<T>) obj;
            if(CollectionUtils.isEmpty(tList)) {
                return ResponseEntity.NO_DATA_FOUND();
            }
        }

        JSONConfig config = JSONConfig.create();
        config.setIgnoreNullValue(false);
        json = JSONUtil.toJsonStr(JSONUtil.parse(obj, config));
        encryptionStr = Sm4Utils.encryptEcb(encryptionKey, json);
        log.info("返回值明文：{}， 密文：{}", json, encryptionStr);

        return ResponseEntity.success(encryptionStr);
    }

    /**
     * ========================================直赔部分==========================================
     * ====================================目前仅有住院部分========================================
     */

    /**
     * 商保直赔身份确认通知
     * @param param
     * @return
     */
    @PostMapping("/confirmUser")
    public ResponseEntity confirmUser(@RequestBody String param) {
        if(StringUtils.isBlank(param)) {
            log.info("入参为null");
            return ResponseEntity.error("参数错误！");
        }

        decryptionStr = Sm4Utils.decryptEcb(encryptionKey, param);
        log.info("加密串：{}， 解密后：{}", param, decryptionStr);

        ConfirmUserParam confirmUserParam = JSONUtil.toBean(decryptionStr, ConfirmUserParam.class);
        if(Objects.isNull(confirmUserParam)) {
            log.info("解密后转换入参对象出现异常");
            return ResponseEntity.error("参数转换错误！");
        }

        ConfirmUserData confirmUserData = service.confirmUser(confirmUserParam);
        return unifyResp(confirmUserData);
    }

    public static void main(String[] args) {
        String key = "EF7E26F248CFB37D256E3F0AB40BFFF8";
        String parm = "{\n" +
                "    \"medicalNum\":\"110938564\"\n" +
                "}";

        String sign = "A911E8230DAD12E5596DE37120E91D18855EE259E730B68E90508321C1E4E4EC910A807D446F8FC0ECA37153F4AE5950";
        System.out.println(Sm4Utils.encryptEcb(key, parm));

        System.out.println(Sm4Utils.decryptEcb(key ,sign));

    }
}